"""
file: conftest.py
Author: 城下秋草
Date: 2024/12/29
Desc: hook的使用
"""
from email.policy import default

import pytest


@pytest.fixture
def testdata(request):
    return request.param

def pytest_generate_tests(metafunc):
    if "testdata" in metafunc.fixturenames:
        if metafunc.config.getoption("--testenv"):
            data = ["测试数据"]
        else:
            data = ["生产数据"]
        metafunc.parametrize("testdata",data,indirect=True)

def pytest_addoption(parser):
    parser.addoption(
        "--testenv",
        action="store_true",
        default=False,
        help="使用测试环境"
    )

def pytest_make_parametrize_id(argname,val):
    return f"{argname}:{val}"

def pytest_collection_modifyitems(config,items):
    if config.getoption("--testenv"):
        for item in items:
            if "testenv" in item.keywords:
                item.add_marker(pytest.mark.skip(reason="非测试环境不执行"))

def pytest_configure(config):
    config.addinivalue_line("markers","testenv:不在测试环境下运行")

def pytest_sessionstart(session):
    print("**********测试开始**************")

def pytest_sessionfinish(session):
    print(f"\r\n**********测试结束, 失败用例数{session.testsfailed}**************")

def pytest_runtest_setup(item):
    print(f"\r\n==========setup {item.name}========")

def pytest_runtest_teardown(item):
    print(f"\r\n==========teardown {item.name}========")